/*******************************************************************************
 * @file Copyright 2009 The CREATE Lab at CMU
 * 
 *       This file is part of JubJub, an open source project.
 *       http://code.google.com/p/jubjub/
 * 
 *       Redistribution and use in source and binary forms, with or without
 *       modification, are permitted provided that the following conditions are
 *       met:
 * 
 *       Redistributions of source code must retain the above copyright notice,
 *       this list of conditions and the following disclaimer.
 * 
 *       Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 * 
 *       Neither the name of Carnegie Mellon nor the names of its contributors
 *       may be used to endorse or promote products derived from this software
 *       without specific prior written permission.
 * 
 *       THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 *       "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 *       LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 *       PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 *       HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 *       SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 *       LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 *       DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 *       THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 *       (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 *       OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *******************************************************************************/

package edu.cmu.ri.createlab.jj.projects;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import edu.cmu.ri.createlab.jj.JubJub.EmptyStringException;
import edu.cmu.ri.createlab.jj.visitors.BVReturn;
import edu.cmu.ri.createlab.jj.visitors.BlockVisitorInterface;

public class JJLibrary
{
   
   ///The name of this library
   private String name;
   /**
    * A list of all the classes contained in this library.
    * Editor can define special classes, such as Flow or Operation
    * to determine what's in the default tabs.
    */
   private List<JJClass> classes;
   ///A list of tabs this library defines
   private List<JJTab> tabs;
   
   ///Create a new library with the given name
   public JJLibrary(String name)
      throws EmptyStringException
   {
      if(name == null || name.length() < 1)
         throw new EmptyStringException("Empty library name");
      this.setName(name);
      this.classes = new LinkedList<JJClass>();
   }
   
   ///Set the name of this library
   public void setName(String name)
   {
      this.name = name;
   }

   ///get the name of this library
   public String getName()
   {
      return name;
   }

   ///add a tab to this project
   public void addTab(JJTab tab)
   {
      if(!hasTab(tab))
         tabs.add(tab);
   }

   ///add a tab to this project
   public void addTab(JJTab tab, int index)
   {
      if(!hasTab(tab))
         tabs.add(index, tab);
   }
   
   ///remove a tab from this project
   public void remTab(JJTab tab)
   {
      tabs.remove(tab);
   }
   
   ///Returns whether the given tab is in this project
   public boolean hasTab(JJTab tab)
   {
      return tabs.contains(tab);
   }
   
   ///Returns whether a tab with the given name is in this project
   public boolean hasTab(String name)
   {
      Iterator<JJTab> it = tabs.iterator();
      while(it.hasNext())
      {
         if(it.next().getName().equals(name))
            return true;
      }
      return false;
   }

   public List<JJTab> getTabs()
   {
      return tabs;
   }

   ///add a class to the library
   public void addClass(JJClass jjclass)
   {
      if(!hasClass(jjclass)) classes.add(jjclass);
   }
   
   ///remove a class from the library
   public void remClass(JJClass jjclass)
   {
      classes.remove(jjclass);
   }
   
   ///returns whether this library contains the given class
   public boolean hasClass(JJClass jjclass)
   {
      return classes.contains(jjclass);
   }
   
   ///returns whether this library contains a class of the given name
   public boolean hasClass(String name)
   {
      Iterator<JJClass> it = classes.iterator();
      while(it.hasNext())
      {
         if(it.next().getName().equals(name))
            return true;
      }
      return false;
   }
 
   ///returns the class with the given name or null if it doesn't exist
   public JJClass getClass(String name)
   {
      Iterator<JJClass> it = classes.iterator();
      while(it.hasNext())
      {
         JJClass tempClass = it.next();
         if(tempClass.getName().equals(name))
            return tempClass;
      }
      return null;
   }
   
   ///returns a list of classes in this library
   public List<JJClass> getClasses()
   {
      return classes;
   }
   
   ///allows BlockVisitors to visit this object
   public BVReturn accept(BlockVisitorInterface bv)
   {
      return bv.visit(this);
   }

}
